package com.haoxuewa.config.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Calendar;
import java.util.Date;

@WebListener
public class OnLineCountListener implements HttpSessionListener {
    public static int[] realTimeOnLineCountList = new int[6];

    @Override
    public void sessionCreated(HttpSessionEvent se) {
//        System.out.println("Session + 1  !!!");
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        switch (hour){
            case 0:
            case 1:
            case 2:
            case 3:
                realTimeOnLineCountList[0]++;
                break;
            case 4:
            case 5:
            case 6:
            case 7:
                realTimeOnLineCountList[1]++;
                break;
            case 8:
            case 9:
            case 10:
            case 11:
                realTimeOnLineCountList[2]++;
                break;
            case 12:
            case 13:
            case 14:
            case 15:
                realTimeOnLineCountList[3]++;
                break;
            case 16:
            case 17:
            case 18:
            case 19:
                realTimeOnLineCountList[4]++;
                break;
            case 20:
            case 21:
            case 22:
            case 23:
                realTimeOnLineCountList[5]++;
                break;
        }
        se.getSession().getServletContext().setAttribute("onLineTotalCount", realTimeOnLineCountList);
//        se.getSession().getServletContext().setAttribute("realTimeOnLineCount", realTimeOnLineCount);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
//        System.out.println("Session - 1  !!!");
//        realTimeOnLineCount--;
//        se.getSession().getServletContext().setAttribute("onLineTotalCount", onLineTotalCount);
//        se.getSession().getServletContext().setAttribute("realTimeOnLineCount", realTimeOnLineCount);
    }
}